package HomeWork;

import HomeWork.Domain.Judge;

import java.util.ArrayList;
import java.util.Scanner;

/*
在唱歌比赛中，可能有多名评委要给选手打分，分数是【0-100】之间的整数，
选手最后得分为：去掉最高分，最低分后剩余的平均分，
请编写程序能够录入多名评委的分数，并算出选手的最后得分。
 */
public class Test03 {
    public static void main(String[] args) {
        ArrayList<Judge> list = new ArrayList<>();

        int count = 0;
        Scanner sc = new Scanner(System.in);
        int score;
        while (true) {
            System.out.println("==========请选择==========");
            System.out.println("1.添加评委得分");
            System.out.println("2.计算平均分");
            String num = sc.next();
            switch (num){
                case "1":
                    while (true) {
                        System.out.println("请输入评委得分：");
                        score = sc.nextInt();
                        if(score < 0 || score > 100){
                            System.out.println("您的输入有误，请重新输入：");
                        }else{
                            Judge jug = new Judge(score);
                            list.add(jug);
                            count++;
                            break;
                        }
                    }
                    break;
                case "2":
                    int sum = 0;
                    if(count < 2){
                        System.out.println("请至少输入三名评委分数！");
                    }else{
                        for (int i = 0; i < list.size(); i++) {
                            Judge jug = list.get(i);
                            sum += jug.getScore();
                        }
                        sum = sum -getMax(list) - getMin(list);
                        System.out.println("平均分为："+sum/(list.size()-2));
                    }
                    System.exit(0);
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }

    }

    public static int getMax(ArrayList<Judge> list){
        int max = list.get(0).getScore();
        for (int i = 1; i < list.size(); i++) {
            Judge jug = list.get(i);
            if(max < jug.getScore()){
                max = jug.getScore();
            }
        }
        return max;
    }

    public static int getMin(ArrayList<Judge> list){
        int min = list.get(0).getScore();
        for (int i = 1; i < list.size(); i++) {
            Judge jug = list.get(i);
            if(min > jug.getScore()){
                min = jug.getScore();
            }
        }
        return min;
    }
}
